<?php
/**
 * Methods shared between admin and reception controllers.
 * @author jan
 */
class RegistrationControllerBase extends Controller {
	
	public function __construct() {
		parent::__construct();
		$this->setDefaultListOrder('id');
		$this->listActionName = 'listing?seminar_id=' . $_REQUEST['seminar_id'];
		
		$this->entityName = 'registration';
	}
	
	public function display() {
		$id = $this->input->get('id');
	
		if (! valid::isId($id)) {
			$this->msg->error('error.invalidId');
			$this->redirectBack();
		}
	
		$this->viewData['registration'] = $this->getSeminarService()->loadRegistrationDisplayData($id);
		if (! $this->viewData['registration']) {
			$this->redirectToList('error.recordNotFound');
		}
		$this->renderMySubview('registrationDisplay');
	}
	
	protected function resolveSeminar() {
		$seminarId = $this->input->get('seminar_id');
		if (! $seminarId) {
			$seminarId = $this->input->post('seminar_id');
		}
		 
		// Basic validation
		if (! valid::isId($seminarId)) {
			$this->msg->error('error-invalidId');
			$this->redirect('/admin/seminar/all');
		}
		 
		$seminar = $this->dao->fetchSeminar($seminarId, $this->getL10nLanguageCode());
		if (! $seminar) {
			$this->msg->error('error-recordNotFound');
			$this->redirect('/admin/seminar/all');
		}
		$this->viewData['seminar'] = $seminar;
		return $seminar;
	}
	
	public function listing() {
		$seminar = $this->resolveSeminar();
		$params = array('seminarId' => $seminar['id']);
		$query = 'SELECT r.*, p.first_name, p.last_name, p.idcard_number, p.idcard_type, pg.name AS price_group_name, pg.currency_code, confirmedBy.first_name AS confirmed_by_first_name, confirmedBy.last_name AS confirmed_by_last_name ' .
				'FROM registration r ' . 
				'JOIN person p ON p.id = r.person_id ' .
				'JOIN price_group pg ON pg.id = r.price_group_id ' .
				'LEFT OUTER JOIN person confirmedBy ON confirmedBy.id = r.confirmed_by_id ' .
				'WHERE r.seminar_id = :seminarId ' .
				'AND cancelled_at IS NULL ';

		$this->initPagination(new QueryAndParams($query, $params));
		
		$this->viewData['registrationList'] = $this->dao->queryAll($this->orderLimitAndOffset($query), $params);
		
		$this->renderMySubview('registrationListing');
	}
	
	public function searchData() {
		$seminar = $this->resolveSeminar();
		
		$this->viewData['seminarBatchOptions'] = $this->dao->queryOptions(
				'SELECT b.batch_code, l10n.name ' .
				'FROM batch b ' .
				'JOIN batch_l10n l10n ON l10n.batch_code = b.batch_code AND l10n.seminar_id = :seminarId AND l10n.language_code = :lang ' .
				'WHERE b.seminar_id = :seminarId ' .
				'ORDER BY l10n.name',
				array('seminarId' => $seminar['id'], 'lang' => $this->getL10nLanguageCode())
		);
		
		$this->viewData['programTypeOptions'] = $this->dao->queryOptions(
				'SELECT l10n.program_type_code, l10n.name ' .
				'FROM available_program_type bpt ' .
				'JOIN batch b ON b.batch_code = bpt.batch_code AND b.seminar_id = bpt.seminar_id ' .
				'JOIN program_type_l10n l10n ON l10n.program_type_code = bpt.program_type_code AND l10n.language_code = :lang ' .
				'WHERE b.seminar_id = :seminarId ' .
				'ORDER BY l10n.name',
				array('seminarId' => $seminar['id'], 'lang' => $this->getL10nLanguageCode())
		);
		
		$this->viewData['paymentOrganizationOptions'] = $this->dao->queryOptions(
				'SELECT DISTINCT o.id, o.name ' .
				'FROM registration r ' .
				'JOIN organization o ON r.payment_organization_id = o.id ' .
				'WHERE r.seminar_id = :seminarId ' .
				'ORDER BY o.name',
				array('seminarId' => $seminar['id']));
		
		// $this->viewData['accommodationTypeOptions'] = $this->dao->queryOptions('');
		$this->viewData['accommodationTypeOptions'] = array();
		
		$this->viewData['genderOptions'] = array(
				'M' => text::get('person.gender-M'),
				'F' => text::get('person.gender-F')
		);
	}
	
	public function togglePresence() {
		$id = $this->input->post('id');
	
		if (! valid::isId($id)) {
			$this->redirectToList('error.invalidId');
		}
	
		$registration = $this->dao->getById('registration', $id);
		if (! $registration) {
			$this->redirectToList('error.recordNotFound');
		}
		
		$registration['is_present'] = ! $registration['is_present'];
		$this->dao->update('registration', $registration);
	
		if (request::is_ajax()) {
			AjaxXmlResponseBuilder::sendUpdate('presence_' . $id, format::twoValueBoolean($registration['is_present']));
		}
		else {
			$participant = $this->dao->getById('person', $viRegistration['person_id']);
			if ($viRegistration['is_present']) {
				$this->msg->info('registration.participantSetPresent', $participant['first_name'] . ' ' . $participant['last_name']);
			}
			else {
				$this->msg->info('registration.participantSetNotPresent', $participant['first_name'] . ' ' . $participant['last_name']);
			}
			$this->redirectToList();
		}
	}
	
	public function keyDepositForm() {
		$registrationId = $this->input->get('id');
		if (! valid::isId($registrationId)) {
			$this->redirectToList('error-invalidId');
		}
		 
		$this->viewData['registration'] = $this->dao->getById('registration', $registrationId);
		if (! $this->viewData['registration']) {
			$this->redirectToList('error-recordNotFound');
		}
		 
		$this->renderMySubview('registrationKeyDeposit');
	}
	
	public function keyDepositSave() {
		$registrationId = $_POST['id'];
		$deposit = $_POST['key_deposit'];
		$isKeyReturned = $_POST['is_key_returned'];
		 
		if (! valid::isId($registrationId)) {
			$this->redirectToList('error-invalidId');
		}
		 
		$registration = $this->dao->getById('registration', $registrationId);
		if (! $registration) {
			$this->redirectToList('error-recordNotFound');
		}
		 
		$registration['key_deposit'] = $deposit;
		if (strlen($deposit) !== 0) {
			$registration['is_key_returned'] = $isKeyReturned;
		}
		$this->dao->update('registration', $registration);
		
		$person = $this->dao->getById('person', $registration['person_id']);
		
		$this->msg->info('registration-keyDeposit-updated', format::personFullName($person), $deposit);
		$this->redirectBack();
	}
	
	public function idCardForm() {
		$personId = $this->input->get('id');
		if (! valid::isId($personId)) {
			$this->redirectToList('error-invalidId');
		}
	
		$this->viewData['person'] = $this->dao->getById('person', $personId);
		if (! $this->viewData['person']) {
			$this->redirectToList('error-recordNotFound');
		}
		$this->viewData['idcardTypeOptions'] = form::idcardOptions();
		 
		$this->renderMySubview('idCardForm');
	}
	
	public function idCardSave() {
		$personId = $_POST['id'];
		$idcardNumber = $_POST['idcard_number'];
		$idcardType = $_POST['idcard_type'];
	
		if (! valid::isId($personId)) {
			$this->redirectToList('error-invalidId');
		}
	
		$person = $this->dao->getById('person', $personId);
		if (! $person) {
			$this->redirectToList('error-recordNotFound');
		}
	
		$person['idcard_number'] = $idcardNumber;
		$person['idcard_type'] = $idcardType;
		$this->dao->update('person', $person);
		 
		$this->msg->info('person-idcard-updated', format::personFullName($person));
		$this->redirectBack();
	}
	
	public function toggleKeyReturned() {
		$id = $this->input->post('id');
		 
		if (! valid::isId($id)) {
			$this->redirectToList('error.invalidId');
		}
		 
		$registration = $this->dao->getById('registration', $id);
		if (! $registration) {
			$this->redirectToList('error.recordNotFound');
		}
		 
		$registration['is_key_returned'] = ! $registration['is_key_returned'];
		$this->dao->update('registration', $registration);
		 
		AjaxXmlResponseBuilder::sendUpdate('keyRet_' . $id, format::twoValueBoolean($registration['is_key_returned']));
	}
	
	public function recIdCardNumber() {
		$personId = $this->input->post('id');
	}
}
